Jomini
A low level, performance oriented parser for save and game files from Paradox Development Studio titles (eg: Europa Universalis (EU4), Hearts of Iron (HOI4), and Crusader Kings (CK3), Imperator, Stellaris, and Victoria).
For an in-depth look at the Paradox Clausewitz format and the pitfalls that come trying to support all variations, consult the write-up. In short, it's extremely difficult to write a robust and fast parser that abstracts over the format difference between games as well as differences between game patches. Jomini hits the sweet spot between flexibility while still being ergonomic.
Jomini is the cornerstone of the online EU4 save file analyzer. This library also powers the Paradox Game Converters and pdxu.
Features
- ✔ Versatile: Handle both plaintext and binary encoded data
- ✔ Fast: Parse data at over 1 GB/s
- ✔ Small: Compile with zero dependencies
- ✔ Safe: Extensively fuzzed against potential malicious input
- ✔ Ergonomic: Use serde-like macros to have parsing logic automatically implemented
- ✔ Embeddable: Cross platform native apps, statically compiled services, or in the browser via Wasm
Quick Start
Below is a demonstration on parsing plaintext data using jomini tools.
use ;
let data = br#"
human = yes
third < 5
forth = 10
core = "HAB"
names = { "Johan" "Frederick" }
core = FRA
"#;
let expected = Model ;
let actual: Model = from_windows1252_slice?;
assert_eq!;
Binary Parsing
Parsing data encoded in the binary format is done in a similar fashion but with a couple extra steps for the caller to supply:
- How text should be decoded (typically Windows-1252 or UTF-8)
- How rational (floating point) numbers are decoded
- How tokens, which are 16 bit integers that uniquely identify strings, are resolved
Implementors be warned, not only does each Paradox game have a different binary format, but the binary format can vary between patches!
Below is an example that defines a sample binary format and uses a hashmap token lookup.
use ;
use ;
;
let data = ;
let mut map = new;
map.insert;
let actual: MyStruct = builder_flavor
.from_slice?;
assert_eq!;
When done correctly, one can use the same structure to represent both the plaintext and binary data without any duplication.
One can configure the behavior when a token is unknown (ie: fail immediately or try to continue).
Caveats
Caller is responsible for:
- Determining the correct format (text or binary) ahead of time
- Stripping off any header that may be present (eg:
EU4txt
/EU4bin
) - Providing the token resolver for the binary format
- Providing the conversion to reconcile how, for example, a date may be encoded as an integer in the binary format, but as a string when in plaintext.
The Mid-level API
If the automatic deserialization via JominiDeserialize
is too high level, there is a mid-level
api where one can easily iterate through the parsed document and interrogate fields for
their information.
use TextTape;
let data = b"name=aaa name=bbb core=123 name=ccc name=ddd";
let tape = from_slice.unwrap;
let reader = tape.windows1252_reader;
for in reader.fields
The mid-level API also provides the excellent utility of converting the
plaintext Clausewitz format to JSON when the json
feature is enabled.
use TextTape;
let tape = from_slice?;
let reader = tape.windows1252_reader;
let actual = reader.json.to_string?;
assert_eq!;
One Level Lower
At the lowest layer, one can interact with the raw data directly via TextTape
and BinaryTape
.
use ;
let data = b"foo=bar";
assert_eq!;
If one will only use TextTape
and BinaryTape
then jomini
can be compiled without default
features, resulting in a build without dependencies.
Write API
There are two targeted use cases for the write API. One is when a text tape is on hand. This is useful when one needs to reformat a document (note that comments are not preserved):
use ;
let tape = from_slice?;
let mut out: = Vec new;
let mut writer = new.from_writer;
writer.write_tape?;
assert_eq!;
The writer normalizes any formatting issues. The writer is not able to losslessly write all parsed documents, but these are limited to truly esoteric situations and hope to be resolved in future releases.
The other use case is geared more towards incremental writing that can be found in melters or those crafting documents by hand. These use cases need to manually drive the writer:
use TextWriterBuilder;
let mut out: = Vec new;
let mut writer = new.from_writer;
writer.write_unquoted?;
writer.write_unquoted?;
writer.write_unquoted?;
writer.write_unquoted?;
assert_eq!;
Unsupported Syntax
Due to the nature of Clausewitz being closed source, this library can never guarantee compatibility with Clausewitz. There is no specification of what valid input looks like, and we only have examples that have been collected in the wild. From what we do know, Clausewitz is recklessly flexible: allowing each game object to potentially define its own unique syntax.
We can only do our best and add support for new syntax as it is encountered.
Benchmarks
Benchmarks are ran with the following command:
cargo clean
cargo bench -- parse
find ./target -wholename "*/new/raw.csv" -print0 | xargs -0 xsv cat rows > assets/jomini-benchmarks.csv
And can be analyzed with the R script found in the assets directory.
Below is a graph generated from benchmarking on an arbitrary computer.